package tools;

import java.util.HashMap;

import org.resotc.model.Centroid;

import ec.util.MersenneTwisterFast;


public class CentroidManager {

	private static CentroidManager cm = null;
	
	// Managed Centroids
	private HashMap<Integer, Centroid> centroids;
	
	private CentroidManager(){
		centroids = new HashMap<Integer, Centroid>();
	}
	
	public static CentroidManager getCentroidManager(){
		if(cm == null){
			cm = new CentroidManager();
		}
		return cm;
	}
	
	public Centroid getCentroidByID(int theID){
		return centroids.get(theID);
	}
	
	public void addNewCentroid(Centroid c){
		if(c != null){
			centroids.put(c.getId(), c);
		} else {
			System.err.println("[CentroidManager]: Cannot add new centroid, argument is null!");
		}
	}
	
	public Centroid getRandomCentroid(){
		int range = centroids.size();

		if(range == 0){
			System.err.println("FUCK!");
			return null;
		} else if(range == 1){
			return (Centroid)centroids.values().toArray()[0];
		} else {
			// Ok, now it should be random...
			MersenneTwisterFast mt = new MersenneTwisterFast();
			
			/**
			 * ToDo: Verify results... is it doing what it should do???
			 */
			int position = mt.nextInt(range);
			// System.out.println("Returning centroid position " + position);

			Centroid myC = centroids.get(centroids.keySet().toArray()[position]);
			
			return myC;
		}
	
	}
}
